import { TGameObject, TGameObjectOptions } from './tGameObject';
import { GameObject } from './gameObject';

interface ContainerOptions extends TGameObjectOptions {
  children?: GameObject[],
}

export class Container extends TGameObject {
  children: GameObject[] = [];

  constructor (options: ContainerOptions) {
    super(options);

    const { children, } = options;
    if (children && children.length > 0) {
      this.children.push(...children);
    }
  }

  render (canvasCtx: CanvasRenderingContext2D) {
    this.children.forEach(child => {
      canvasCtx.save();
      child.render(canvasCtx);
      canvasCtx.restore();
    });
  }

  addChild (child: GameObject) {
    this.children.push(child);
  }

  removeChild (child: GameObject) {
    const targetIndex = this.children.findIndex(el => el === child);
    if (targetIndex !== -1) {
      this.children.splice(targetIndex, 1);
    }
  }
}
